import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import '../plug/wlist.dart';

class horizontalPlay extends StatefulWidget {
  final childs;
  final playAction;
  final String url;

  horizontalPlay({@required this.childs, @required this.playAction, this.url, Key key}) : super(key: key);

  @override
  _VideoAppState createState() => _VideoAppState();
}

class _VideoAppState extends State<horizontalPlay> {
  VideoPlayerController _controller;
  bool _isPlaying = false;
  bool _isLoad = false;
  bool _isResoult = false;
  String timeLength = "0.0";
  String curTime = "0.0";
  Duration prevTime; //上一个时间
  double progress_bar = 0.0; //进度条长度
  double other_width = 0.0; //其他长度

  double height = 0.0; //屏幕高
  double width = 0.0; //屏幕宽

  double setHeight = 250.0;

  double curWidth = 0.0; //当前高度
  double curHeight = 0.0; //当前高度
  bool _isfull = false;
  Widget tabView;

  var cc=11111;
  @override
  void initState() {
    super.initState();
    print("当前Url：${widget.url}");
    print(widget.url != "");

    if (widget.url != "") {
      _controller = VideoPlayerController.network(
        widget.url,
        //      'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4',
      )
        ..addListener(() {
          _isLoad = true; //
          timeLength = _controller.value.duration.toString().split(".")[0];
          _isResoult = true;
          setState(() {
            curTime = _controller.value.position.toString().split(".")[0];
          });
//        prevTime = _controller.value.position;
          final bool isPlaying = _controller.value.isPlaying;

          if (isPlaying != _isPlaying) {
            setState(() {
              _isPlaying = isPlaying;
            });
          }
        })
        ..initialize().then((_) {
          // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
          setState(() {});
        });
    }
    widget.playAction(cc);
  }

  @override
  void dispose() {
    _controller.dispose();
    // TODO: implement dispose
    super.dispose();
  }

  void full() {
    height = MediaQuery.of(context).size.height;
    width = MediaQuery.of(context).size.width;
    progress_bar = (width * 0.5); //进度条长度
    other_width = ((width - progress_bar) / 4); //其他长度
    if (_isfull) {
      tabView = _tabView(status: false);
      curHeight = height;
      curWidth = width;
      // 全屏（隐藏status bar 和Navitation Bar）
      SystemChrome.setEnabledSystemUIOverlays([]);
      // 设置横屏
      SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
    } else {
      curHeight = setHeight;
      curWidth = width;
      // 退出全屏
      SystemChrome.setEnabledSystemUIOverlays(
          [SystemUiOverlay.bottom, SystemUiOverlay.top]);
      // 返回竖屏
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
      tabView = _tabView();
    }
  }

  @override
  Widget build(BuildContext context) {
    full();


    return new DefaultTabController(
      length: 10,
      child: new Scaffold(
        backgroundColor: Color.fromRGBO(234, 234, 234, 1.0),
        body: new Column(
          children: <Widget>[
            new Center(
                child: new Container(
              height: curHeight,
              width: curWidth,
              color: Colors.black,
              child: new Stack(
                children: <Widget>[
                  new Container(
                    alignment: Alignment.center,
                    child: _isResoult
                        ? new Stack(
                            children: <Widget>[
                              new Container(
                                alignment: Alignment.center,
                                child: _controller.value.initialized
                                    ? AspectRatio(
                                        aspectRatio:
                                            _controller.value.aspectRatio,
                                        child: VideoPlayer(_controller),
                                      )
                                    : Container(),
                              ),
                              new Offstage(
                                offstage: _isLoad,
                                child: new Align(
                                  child: new CupertinoActivityIndicator(
                                    animating: true,
                                  ),
                                ),
                              ),
                            ],
                          )
                        : new Align(
                            child: new CupertinoActivityIndicator(
                              animating: true,
                            ),
                          ),
                  ),
                  new Positioned(
                      bottom: 10.0,
                      width: width,
                      child: _isResoult
                          ? new Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.center,
                              mainAxisSize: MainAxisSize.min,
                              children: <Widget>[
                                new Container(
                                  alignment: Alignment.center,
//                         color: Colors.blue,
                                  width: other_width,
                                  child: new InkWell(
                                    child: new Icon(
                                      _controller.value.isPlaying
                                          ? Icons.pause
                                          : Icons.play_arrow,
                                      color: Colors.white,
                                    ),
                                    onTap: _controller.value.isPlaying
                                        ? _controller.pause
                                        : _controller.play,
                                  ),
                                ),
                                new Container(
//                         color: Colors.orange,
                                  alignment: Alignment.center,
                                  width: other_width,
                                  child: new Text(
                                    curTime,
                                    style: new TextStyle(
                                        color: Colors.white, fontSize: 11.0),
                                  ),
                                ),
                                new Container(
                                  alignment: Alignment.topCenter,
                                  margin: EdgeInsets.only(bottom: 5.0),
                                  width: progress_bar,
                                  child: VideoProgressIndicator(
                                    _controller,
                                    allowScrubbing: true,
                                    colors: VideoProgressColors(
                                      playedColor:
                                          Color.fromRGBO(251, 114, 153, 1.0),
                                    ),
                                  ),
                                ),
                                new Container(
//                         color: Colors.blueAccent,
                                  alignment: Alignment.center,
                                  width: other_width,
                                  child: new Text(
                                    timeLength,
                                    style: new TextStyle(
                                        color: Colors.white, fontSize: 11.0),
                                  ),
                                ),
                                new Container(
                                  alignment: Alignment.center,
                                  width: other_width,
//                         color: Colors.orangeAccent,
                                  child: new InkWell(
                                    child: Icon(
                                      Icons.fullscreen,
                                      color: Colors.white,
                                    ),
                                    onTap: () {
                                      _isfull = !_isfull;
                                      full();
                                    },
                                  ),
                                )
                              ],
                            )
                          : new Text('')),
                  new Positioned(
                      top: _isfull ? 05.0 : MediaQuery.of(context).padding.top,
                      width: width,
                      child: new Row(
                        children: <Widget>[
                          new Container(
//                         color: Colors.blue,
                            child: IconButton(
                                icon:
                                    Icon(Icons.arrow_back, color: Colors.white),
                                onPressed: () {
                                  if (_isfull) {
                                    _isfull = !_isfull;
                                    full();
                                  } else {
                                    Navigator.pop(context);
                                  }
                                }),
                          ),
                          new Container(
                            alignment: Alignment.center,
                            child: new Text(
                              "正在播放",
                              style: new TextStyle(
                                  color: Colors.white, fontSize: 15.0),
                            ),
                          ),
                        ],
                      )),
                ],
              ),
            )),
            new Offstage(offstage: _isfull, child: tabView),
          ],
        ),
      ),
    );
  }
  var play=(){
    print("播放Url：21232");
//    print("播放Url：${widget.url}");
  };



  Widget _tabView({status: true}) {
    if (status) {
      return new Container(
        height: height - curHeight.abs(),
        child: widget.childs(width, (height - curHeight.abs())),
      );
    } else {
      return new Container(
        child: new Text(''),
      );
    }
  }
}
